package com.ellwood.copyright.modules.system.logic;

import com.ellwood.copyright.common.core.enums.HttpCodeEnum;
import com.ellwood.copyright.common.core.exception.ServerException;
import com.ellwood.copyright.common.core.utils.EllAESUtil;
import com.ellwood.copyright.common.core.utils.EllIdUtil;
import com.ellwood.copyright.common.core.utils.EllStrUtils;
import com.ellwood.copyright.infrastructure.system.entity.SysUserEntity;
import com.ellwood.copyright.infrastructure.system.mapper.SysUserMapper;
import com.ellwood.copyright.infrastructure.system.query.SysUserQuery;
import com.ellwood.copyright.infrastructure.system.vo.SysUserDataVO;
import com.ellwood.copyright.modules.system.convert.SysUserConvert;
import com.ellwood.copyright.modules.system.service.SysUserService;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @ProjectName: ell-copyright-pro
 * @PackageName: com.ellwood.copyright.modules.system.logic
 * @ClassName: SysUserLogic
 * @Description: // 用户业务逻辑处理
 * @Author: Ellwood
 * @CreateDate: 2024/11/22 - 11:26
 * @Version: V1.0.0
 */
@AllArgsConstructor
@Component
public class SysUserLogic {

    private final SysUserMapper userMapper;

    /**
     * 查询参数处理
     * @param userQuery 查询参数
     * @return 返回查询的map参数
     */
    public Map<String,Object> findAllParams(SysUserQuery userQuery){
        Map<String, Object> params = new HashMap<>();
        params.put("account",userQuery.getAccount() );
        params.put("gender",userQuery.getGender() );
        params.put("phone",userQuery.getPhone() );

        // todo 数据权限以及过滤处理要等后期加入权限控制之后再处理

        return params;
    }

    /**
     * 新增用户参数处理
     * @param userVo 用户信息
     * @return 返回校验后参数
     */
    public SysUserEntity getUserParams(SysUserDataVO userVo){
        // 新增用户账号不能为空
        if(EllStrUtils.isBlank(userVo.getAccount())) throw new ServerException(HttpCodeEnum.USERNAME_NOT_NULL);
        //  新增用户手机号不能为空
        if(EllStrUtils.isBlank(userVo.getPhone())) throw new ServerException(HttpCodeEnum.MOBILE_NUMBER_NOT_NULL);

        SysUserEntity userEntity= SysUserConvert.INSTANCE.convert(userVo);

        // 参数校验
        SysUserEntity account=userMapper.getByAccount(userEntity.getAccount());
        if(account!=null) throw new ServerException(HttpCodeEnum.USERNAME_EXIST);
        SysUserEntity phone=userMapper.getByPhone(userEntity.getPhone());
        if(phone!=null) throw new ServerException(HttpCodeEnum.MOBILE_NUMBER_EXIST);

        return userEntity;
    }

}